import js from '@eslint/js'
import globals from 'globals'
import tseslint from 'typescript-eslint'
import pluginVue from 'eslint-plugin-vue'
import { defineConfig } from 'eslint/config'

// 只保留代码质量规则，格式规则交给 Prettier
const rules = {
  // 代码质量规则
  curly: [2, 'all'],
  eqeqeq: [2, 'always', { null: 'ignore' }],
  'no-else-return': [1, { allowElseIf: false }],
  'no-loop-func': 2,
  'no-restricted-syntax': [
    1,
    {
      selector: "BinaryExpression[operator='instanceof']",
      message: "Use 'instanceof' for object type detection.",
    },
    {
      selector: "BinaryExpression[operator='typeof']",
      message: "Use 'typeof' for type detection.",
    },
    {
      selector: "CallExpression[callee.name='parseInt']",
      message:
        'Use Math.floor, Math.round, or Math.ceil instead of parseInt to remove decimal points.',
    },
  ],
  'no-implicit-coercion': [1, { allow: ['!!'] }],
  radix: [2, 'always'],
  'no-array-constructor': 2,
  'no-eval': 2,
  'prefer-const': 1,
  'no-var': 1,
  'prefer-destructuring': [1, { object: true, array: false }],
  'prefer-template': 1,
  'no-duplicate-imports': 2,

  // TypeScript 特定规则
  '@typescript-eslint/no-unused-vars': 'error',
  '@typescript-eslint/explicit-module-boundary-types': 'off',
  '@typescript-eslint/consistent-type-imports': 'error',
  // 允许在某些情况下使用 any
  '@typescript-eslint/no-explicit-any': 'warn', // 或者设置为 'warn' 而不是 'error'

  // 关闭vue组件名多词命名检查配置
  'vue/multi-word-component-names': [
    'warn',
    {
      // 指定需要忽略的组件名称
      ignores: ['index'],
    },
  ],

  // 逻辑改进
  'no-lonely-if': 'error',
  'no-useless-return': 'error',

  // 现代JS推荐
  'arrow-body-style': ['error', 'as-needed'],
}

export default defineConfig([
  // 基本配置
  {
    files: ['**/*.{js,mjs,cjs,ts,mts,cts,vue}'],
    ignores: [
      '**/*.config.js',
      'dist/**',
      'node_modules/**',
      'coverage/**',
      '*.min.js',
      '**/__tests__/**',
      '**/__mocks__/**',
    ],
    languageOptions: {
      globals: {
        ...globals.browser,
        ...globals.node,
        withDefaults: 'readonly',
        defineExpose: 'readonly',
        defineEmits: 'readonly',
        defineProps: 'readonly',
      },
      parserOptions: {
        ecmaVersion: 2021,
        sourceType: 'module',
      },
    },
    rules: rules,
  },
  // JS 推荐配置
  js.configs.recommended,
  // TypeScript 配置
  ...tseslint.configs.recommended,
  // Vue 配置
  pluginVue.configs['flat/essential'],
  // Vue 文件的 TypeScript 解析器
  {
    files: ['**/*.vue'],
    languageOptions: {
      parserOptions: {
        parser: tseslint.parser,
      },
    },
  },
])
